package com.springboot.common.controller;

import com.springboot.util.TenantThreadLocal;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: TongRui乀
 * @Date: 2019-11-15 17:19
 * @description：  定义MVC拦截器处理租户ID
 */
@Configuration
public class MyCatIntercept implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

                TenantThreadLocal.set(request.getHeader("tenantId"));

                return true;
            }
        }).addPathPatterns("/**");
    }
}
